//
//  CarDetailController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/20.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "CarDetailController.h"
#import "PersonalController.h"
#import "CarPathController.h"
#import "ItemMeunView.h"
#import "CarInfoController.h"
#import "FaultDetectController.h"
#import "CarDetailController.h"
#import "CarPathController.h"
#import "UIBarButtonItem+button.h"
#import "LightButton.h"
#import "BatteryView.h"
#import "DialPlateView.h"
#import "GearsView.h"
#import "AlertLockTool.h"
#import "UIView+shadow.h"
#import "BaseSendData.h"
#import "CarTravelModel.h"
#import "PasswordController.h"
#import "TrackMananger.h"

@interface CarDetailController ()<ItemMeunViewDelegate,LightButtonDelegate,GearsViewDelegate>

@property(nonatomic, strong)LightButton *lightBtn;

@property(nonatomic, strong)BatteryView *battery;
@property(nonatomic, strong)DialPlateView *dialView;

//@property(nonatomic, assign)BOOL login;

@property(nonatomic, strong)UILabel *lightLabel;
@property(nonatomic, strong)AlertLockTool *tool;

@property(nonatomic, weak)GearsView *gearsView;

@property(nonatomic, weak)ItemMeunView *lockView;

@property(nonatomic, strong)NSTimer *timer;

@end

@implementation CarDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =[NSString stringWithFormat:@"%@",[BalanceBuleManager shareManager].connectPeripheral.name];
    // Do any additional setup after loading the view.
    UIBarButtonItem *leftItem = [UIBarButtonItem initWithFrame:CGRectMake(0, 0, 22, 22) image:[UIImage imageNamed:@"nav_icon_car"] target:self action:@selector(carInfoAction:)];
     self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem *rightItem = [UIBarButtonItem initWithFrame:CGRectMake(0, 0, 22, 22) image:[UIImage imageNamed:@"nav_icon_map"] target:self action:@selector(mapInfoAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(travelDetail:) name:kPeripheraDidReadDataNotification object:nil];
    [self establishSubViews];
    [BaseSendData sendTravelParamsOrder];
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerAction) userInfo:self repeats:YES];
    [[TrackMananger shareManager] startRun];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    self.dialView.unitLabel.text = [NSString stringWithFormat:@"%@/h",[SwitcherTool shareManager].unit];
  
}

- (void)carInfoAction:(UIButton *)button{
    
    PersonalController *col = [[PersonalController alloc] init];
    
    [self.navigationController pushViewController:col animated:YES];
}

- (void)mapInfoAction:(UIButton *)button{
    
    //[BaseSendData sendTravelParamsOrder];
    CarPathController *col = [[CarPathController alloc] init];
    
    [self.navigationController pushViewController:col animated:YES];
}

- (void)establishSubViews{
    
    self.lightBtn = [LightButton buttonWithType:UIButtonTypeCustom];
    self.lightBtn.delegate = self;
    [self.view addSubview:self.lightBtn];
    [self.lightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.top.mas_offset(16);
        make.width.height.mas_offset(40);
        
    }];
    self.lightLabel = [[UILabel alloc] init];
    self.lightLabel.textAlignment = NSTextAlignmentCenter;
    self.lightLabel.layer.cornerRadius = 6;
    self.lightLabel.layer.borderWidth = 1;
    self.lightLabel.adjustsFontSizeToFitWidth = YES;
    self.lightLabel.layer.masksToBounds = YES;
    [self lightClose];
    self.lightLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.lightLabel];
    [self.lightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.lightBtn.mas_centerY).offset(0);
        make.left.mas_equalTo(self.lightBtn.mas_right).offset(5);
        make.width.mas_offset(72);
        make.height.mas_offset(24);
    }];
    self.battery = [[BatteryView alloc] init];
    [self.view addSubview:self.battery];
    [self.battery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.top.mas_offset(16);
        make.width.mas_offset(80);
        make.height.mas_offset(30);
    }];
    self.battery.batteryPercentage = 0.19;
    CGFloat indexDialHeight = 250;
    self.dialView = [[DialPlateView alloc]init];
    [self.view addSubview:self.dialView];
    [self.dialView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_offset(60);
        make.centerX.mas_offset(0);
        make.height.width.mas_offset(indexDialHeight);
    }];
  
    GearsView *gearView = [[GearsView alloc] init];
    [self.view addSubview:gearView];
    gearView.delegate = self;
    self.gearsView = gearView;
    [gearView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.dialView.mas_bottom).offset(16);
        make.centerX.mas_offset(0);
        make.height.mas_offset(40);
        make.width.mas_offset(140);
    }];
    
    ItemMeunView *item1 = [[ItemMeunView alloc] initWithFrame:CGRectZero imageName:@"fun_icon_bluetooth" bottomTitle:XHLocalizedString(@"Bluetooth")];
    item1.tag = 100;
    item1.delegate = self;
    [self.view addSubview:item1];
    ItemMeunView *item2 = [[ItemMeunView alloc] initWithFrame:CGRectZero imageName:@"fun_icon_lock" bottomTitle:XHLocalizedString(@"Lock/Unclock")];
    item2.tag = 101;
    self.lockView = item2;
    
    item2.delegate = self;
    [self.view addSubview:item2];
    ItemMeunView *item3 = [[ItemMeunView alloc] initWithFrame:CGRectZero imageName:@"fun_icon_check" bottomTitle:XHLocalizedString(@"Detect")];
    item3.delegate = self;
    item3.tag = 102;
    [self.view addSubview:item3];
    
    NSArray *items = @[item1,item2,item3];
    [items mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(gearView.mas_bottom).offset(50);
        make.height.mas_offset(60);
    }];
    [items mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:50 tailSpacing:50];
    for (ItemMeunView *itemView in items) {
        itemView.centerGap = 10;
        
        itemView.bottomFont = [UIFont systemFontOfSize:17];
    }
    
}


- (void)gearsView:(GearsView *)gearsView gears:(NSInteger)gears{
    __weak typeof(self) weakSelf = self;
    //[LTCSingleHud showLoading:nil];
    [[BaseSendData shareManager] sendGearsWithByte:gears complete:^(NSInteger code) {
        if (code == 100) {
            return;
        }
        //[LTCSingleHud hideHud];
        if (code == 1) {
            
            weakSelf.gearsView.gears = gears;
            
        }else{
           // [LTCSingleHud showMessage:@"操作失败"];
        }
        
    }];
    
}

- (void)itemMeunViewAction:(ItemMeunView *)meunView{
    
    switch (meunView.tag) {
        case 100:
        {
            [[BalanceBuleManager shareManager] cancePeripherals];
        }
            break;
        case 101:{
            
            Byte lockbyte = 0x00;
            if ([BaseAcceptData shareManager].travelModel.isLock) {
                
                lockbyte = 0x00;
                
            }else{
                
                lockbyte = 0x01;
                
            }
            [[BaseSendData shareManager] sendLockCarWithByte:lockbyte complete:^(NSInteger code) {
                
                if (code == 100) {
                    
                    return;
                }
                
                if (code == 1) {
                    
                    [BaseSendData sendTravelParamsOrder];
                    
                }else{
                    
                    // [LTCSingleHud showMessage:@"操作失败"];
                }
            }];
           
            
        }
            break;
        case 102:{
            
            FaultDetectController *col = [[FaultDetectController alloc] init];
            
            [self.navigationController pushViewController:col animated:YES];
            
        }
            break;
        default:
            break;
    }
    
    
}

- (void)lightbutton:(LightButton *)lightBtn touchState:(LightButtonState)lightState{
   
    Byte byte = 0x00;
    switch (lightState) {
        case LightButtonOpen:
        {
            byte = 0x01;
        }
            break;
        case LightButtonAuto:{
            
            byte = 0x02;
            
        }
            break;
        case LightButtonClose:{

            byte = 0x00;
            
        }
            break;
        default:
            break;
    }
   // [LTCSingleHud showLoading:nil];
    __weak typeof(self) weakSelf = self;
    [[BaseSendData shareManager] sendCarLightWithByte:byte complete:^(NSInteger code) {
        
        if (code == 100) {
            return ;
        }else{
            
            [LTCSingleHud hideHud];
            
            [weakSelf lightWithCode:code];
        }
        
    }];

}


- (void)lightWithCode:(NSInteger)code{
    
    switch (code) {
        case 0:
            
            [self lightClose];
            self.lightBtn.lightState = LightButtonClose;
            break;
        case 1:{
            
            [self lightOpen];
            self.lightBtn.lightState = LightButtonOpen;
        }
            break;
            
        case 2:{
            
            [self lightAuto];
            self.lightBtn.lightState = LightButtonAuto;
        }
            break;
        default:
            break;
    }
    
}

- (void)lightOpen{
    self.lightLabel.text = XHLocalizedString(@"open");
    self.lightLabel.layer.borderColor = [UIColor colorFromHexCode:@"#c6eeaf"].CGColor;
    self.lightLabel.backgroundColor = [UIColor colorFromHexCode:@"#f0fbeb"];
    self.lightLabel.textColor = [UIColor colorFromHexCode:@"#6fc94a"];
}


- (void)lightClose{
    self.lightLabel.text = XHLocalizedString(@"close");
    self.lightLabel.layer.borderColor = [UIColor colorFromHexCode:@"#dddddd"].CGColor;
    self.lightLabel.backgroundColor = [UIColor colorFromHexCode:@"#f2f2f2"];
    self.lightLabel.textColor = [UIColor colorFromHexCode:@"#cccccc"];
    
}

- (void)lightAuto{
    
    self.lightLabel.text = XHLocalizedString(@"automation");
    self.lightLabel.layer.borderColor = [UIColor colorFromHexCode:@"#565656"].CGColor;
    self.lightLabel.backgroundColor = [UIColor colorFromHexCode:@"#666666"];
    self.lightLabel.textColor = [UIColor colorFromHexCode:@"#ffffff"];
}


- (void)assgiaTravelParam{
    
    self.battery.batteryPercentage = [BaseAcceptData shareManager].travelModel.power / 100.0;
    self.dialView.speed = [BaseAcceptData shareManager].travelModel.speed;
    if ([BaseAcceptData shareManager].travelModel.islightAUTO) {
        
        [self lightAuto];
        self.lightBtn.lightState = LightButtonAuto;
        
    }else if ([BaseAcceptData shareManager].travelModel.islightONOFF){
        
        [self lightOpen];
          self.lightBtn.lightState = LightButtonOpen;
        
    }else{
        
        [self lightClose];
        self.lightBtn.lightState = LightButtonClose;
    }
    
    if ([BaseAcceptData shareManager].travelModel.isLock) {
        
         self.lockView.image = [UIImage imageNamed:@"fun_icon_lock"];
        
    }else{
        
        self.lockView.image = [UIImage imageNamed:@"fun_icon_unlock"];
    }
    
}


- (void)travelDetail:(NSNotification *)notice{
    ResultModel *model = notice.object;
    if (model.dataType == TravelParamType) {
        
         [self assgiaTravelParam];
        
    }
   
    
}

- (void)timerAction{
     [BaseSendData sendTravelParamsOrder];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
